package com.mes.cloud.service;

import com.mes.cloud.api.dto.request.organization.SysOrganizationChannelRequest;
import com.mes.cloud.api.dto.response.organization.SysOrganizationChannelDto;
import com.mes.cloud.exception.BaseException;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.constant.Constants;
import com.mes.cloud.dao.SysOrganizationChannelDao;
import com.mes.cloud.entity.SysOrganizationChannel;
import com.mes.cloud.entity.SysOrganizationLog;
import com.mes.cloud.entity.vo.organization.SysOrganizationChannelVo;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.util.DateUtil;
import com.mes.cloud.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 组织渠道中间表
 *
 * @author ysd
 * @email shaodong.yuan@pactera.com
 * @version 2019-08-30 13:36:07
 */
@Service
public class SysOrganizationChannelService {

    private SysOrganizationChannelDao sysOrganizationChannelDao;

    private IPKeyGenerator iPKeyGenerator;

    private LogService logService;

    @Autowired
    public SysOrganizationChannelService(SysOrganizationChannelDao sysOrganizationChannelDao , IPKeyGenerator iPKeyGenerator,LogService logService){
        this.sysOrganizationChannelDao = sysOrganizationChannelDao;
        this.iPKeyGenerator = iPKeyGenerator;
        this.logService = logService;
    }

    /**
    * 查列表
    * @param sysOrganizationChannelRequest
    * @return JsonResult
    */
    public JsonResult<List<SysOrganizationChannelDto>> list(SysOrganizationChannelRequest sysOrganizationChannelRequest) {
        //查询条件
        SysOrganizationChannel selectParam=new SysOrganizationChannel();
        BeanUtils.copyProperties(sysOrganizationChannelRequest,selectParam);
        List<SysOrganizationChannelVo> sysOrganizationChannelList = sysOrganizationChannelDao.findAllByOragId(selectParam);
        List<SysOrganizationChannelDto> sysOrganizationChannelDtoList = new ArrayList<>();
        for (SysOrganizationChannelVo sysOrganizationChannelVo: sysOrganizationChannelList){
            SysOrganizationChannelDto sysOrganizationChannelDto = new SysOrganizationChannelDto();
            BeanUtils.copyProperties(sysOrganizationChannelVo,sysOrganizationChannelDto);
            sysOrganizationChannelDtoList.add(sysOrganizationChannelDto);
        }
        JsonResult<List<SysOrganizationChannelDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(sysOrganizationChannelDtoList);
        return jsonResult;
    }

    /**
    *  新增
    * @param sysOrganizationChannelRequest
    * @return JsonResult
    */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult add(SysOrganizationChannelRequest sysOrganizationChannelRequest) {
        JsonResult jsonResult = new JsonResult();
        //查询该组织已存在的所属渠道
        SysOrganizationChannel selectParam = new SysOrganizationChannel();
        selectParam.setOrganizationId(sysOrganizationChannelRequest.getOrganizationId());//未删除
        selectParam.setDelFlag(Constants.DEL_FLAG_NO);//未删除
        List<SysOrganizationChannel> resultList = sysOrganizationChannelDao.select(selectParam);

        //更新已存在的所属渠道状态（逻辑删除）
        if (!CollectionUtils.isEmpty(resultList)) {
            selectParam.setUpdateBy(UserUtil.getUpdateByName());
            selectParam.setUpdateByNo(UserUtil.getUpdateByNo());
            selectParam.setUpdateDateStr(DateUtil.updateDateStr());
            selectParam.setDelFlag(Constants.DEL_FLAG_YES);
            sysOrganizationChannelDao.updateByPositionId(selectParam);
        }

        List<String> channelIds = sysOrganizationChannelRequest.getChannelIds();

        if (!CollectionUtils.isEmpty(channelIds)) {
            List<SysOrganizationChannel> spoa = new ArrayList<>();
            for (String channelId : channelIds) {
                SysOrganizationChannel sysOrganizationChannel = new SysOrganizationChannel();
                sysOrganizationChannel.setId(iPKeyGenerator.getGenerateKey());
                sysOrganizationChannel.setOrganizationId(sysOrganizationChannelRequest.getOrganizationId());
                sysOrganizationChannel.setChannelId(channelId);
                sysOrganizationChannel.setUpdateDateStr(DateUtil.updateDateStr());
                sysOrganizationChannel.setCreateBy(UserUtil.getCreateByName());
                sysOrganizationChannel.setUpdateBy(UserUtil.getUpdateByName());
                sysOrganizationChannel.setCreateByNo(UserUtil.getCreateByNo());
                sysOrganizationChannel.setUpdateByNo(UserUtil.getUpdateByNo());
                spoa.add(sysOrganizationChannel);
            }
            int count = sysOrganizationChannelDao.batchInsert(spoa);
            if (count <= 0) {
                throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
            }
        }
        this.insertLog(sysOrganizationChannelRequest.getOrganizationId(), Constants.ADD_LINK_TO_ORGANIZATION);
        return jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode());
    }

    /**
     * @Author ysd
     * @Description <插入组织日志>
     * @Date 16:47 2019/8/30
     * @Param [oraganizationId, logMessage]
     * @return int
     **/
    private int insertLog(String oraganizationId, String logMessage) {
        SysOrganizationLog log = new SysOrganizationLog();
        log.setOrganizationId(oraganizationId);
        log.setMenuCode(Constants.EMPLOYEE_MENU_CODE);
        log.setLog(logMessage);
        log.setId(iPKeyGenerator.getGenerateKey());
        log.setUpdateDateStr(DateUtil.updateDateStr());
        log.setCreateBy(UserUtil.getCreateByName());
        log.setUpdateBy(UserUtil.getUpdateByName());
        log.setCreateByNo(UserUtil.getCreateByNo());
        log.setUpdateByNo(UserUtil.getUpdateByNo());
        return logService.insertOrganizationLog(log);
    }
}
